package MySQL

import (
	"fmt"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB_conn_for_udcpp *gorm.DB
var DB_DSN_for_udcpp string

func Init(db_name, db_account, db_addr string) {
	fmt.Println("init MySQL...")
	DB_DSN_for_udcpp = fmt.Sprintf("%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", db_account, db_addr, db_name)
	var err error
	DB_conn_for_udcpp, err = gorm.Open(mysql.Open(DB_DSN_for_udcpp), &gorm.Config{})
	if err != nil {
		panic(err)
	} else {
		fmt.Println("database connected!")
	}

	// 迁移价格记录
	line := Price_line{}
	err = DB_conn_for_udcpp.AutoMigrate(&line)
	if err != nil {
		panic(err)
	} else {
		fmt.Println("migrate price_lines succeed!")
	}
	// 迁移模型服务记录
	model_server := Model_server{}
	err = DB_conn_for_udcpp.AutoMigrate(&model_server)
	if err != nil {
		panic(err)
	} else {
		fmt.Println("migrate model_server succeed!")
	}
}
